package net.jiug.email.controller;

import net.jiug.common.exception.BadRequestException;
import net.jiug.common.web.base.BaseController;
import net.jiug.email.model.EmailContent;
import net.jiug.email.service.EmailService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.logging.Level;

/**
 * Describe: 控制器
 */
@Controller
@RequestMapping("/email")
public class EmailController extends BaseController {

    @Resource
    private EmailService emailService;

    /**
     * 发送请求
     */
    @RequestMapping("/send/{uuid}")
    @ResponseBody
    public ResponseEntity<Object> send(@PathVariable("uuid") String uuid, @RequestBody EmailContent emailContent) {
        try {
            // 安全检查
            securityVerification(uuid);
            // 业务逻辑
            emailService.notice(emailContent);
        } catch (BadRequestException e) {
            return new ResponseEntity<>(e.getMessage(),HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            logger.log(Level.SEVERE,
                    "An error occurred on line " + e.getStackTrace()[0].getLineNumber() + ": " + e.getMessage(), e);
            return new ResponseEntity<>(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 测试请求
     */
    @RequestMapping("/jiug")
    @ResponseBody
    public ResponseEntity<Object> jiug() {

        try {
            emailService.notice("13245","wakinghhh@163.com");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ResponseEntity<>(HttpStatus.OK.getReasonPhrase(),HttpStatus.OK);
    }

}
